1
提示工程:生成式人工智能的主要交互界面
AI011Lesson 2
00:00

提示工程基础

提示工程(PE) 是设计和优化文本输入以引导大型语言模型(LLMs)生成高质量、一致结果的过程。

1. 定义交互界面

是什么: 它作为生成式人工智能的主要“编程”接口。
为什么: 它将交互从原始且不可预测的文本生成,转变为有目的、结构化的指令执行。

2. 模型基础

  • 基础大模型(Base LLMs): 仅通过大规模数据集中的统计关系训练,以最大化概率 $P(w_t | w_1, w_2, ..., w_{t-1})$ 来预测下一个标记。
  • 指令微调大模型(Instruction-Tuned LLMs): 通过人类反馈强化学习(RLHF)进行微调,使其明确遵循特定指令并充当有用的助手。

3. 成功提示的构成要素

如何: 一个有效的提示通常包含:

  • 指令: 所需执行的具体操作。
  • 主要内容: 需要处理的目标数据。
  • 次要内容: 参数、格式或约束条件(用于应对随机性和幻觉问题)。
分词的真实情况
模型并不读取单词;它们处理的是 标记(tokens)——更小的文本单元,用于计算统计概率。
prompt_structure.py
TERMINALbash — 80x24
> Ready. Click "Run" to execute.
>
Question 1
What is the primary difference between a Base LLM and an Instruction-Tuned LLM?
Base LLMs only process code, while Instruction-Tuned LLMs process natural language.
Instruction-Tuned models are refined through human feedback to follow specific directions, whereas Base LLMs focus on statistical token prediction.
Base LLMs use tokens, but Instruction-Tuned LLMs read whole words at a time.
There is no difference; they are two terms for the exact same architecture.
Question 2
Why is the use of delimiters (like triple backticks or hashes) considered a best practice in prompt engineering?
They reduce the token count, making the API call cheaper.
They force the model to output in JSON format.
To separate instructions from the content the model needs to process, preventing 'separation of concerns' issues.
They increase the model's temperature setting automatically.
Challenge: Tutor AI Constraints
Refining prompts for educational safety.
You are building a tutor-style AI for a startup. The model is currently giving away answers too quickly and sometimes making up facts when it doesn't know the answer.
AI Tutor Interface
Task 1
Implement "Chain-of-thought" prompting in the system message to prevent the AI from giving away answers immediately.
Solution:
Instruct the model to: "Work through the problem step-by-step before providing the final answer. Do not reveal the final answer until the student has attempted the steps."
Task 2
Apply an "out" to prevent fabrications (hallucinations) when the AI doesn't know the answer.
Solution:
Add the explicit instruction: "If you do not know the answer based on the provided text or standard curriculum, state clearly that you do not know."